package com.jweb.framework.core.listener;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.jweb.framework.core.service.ServiceHolder;
import com.jweb.framework.core.service.ServiceRegistry;
/**
 * 修改人： Thomas
 * 修改内容：新增 
 * 类说明：服务注册监听
 */
public class ServiceRegistryListener implements ReferenceListener , ApplicationContextAware{
	
	private Map<String, ServiceHolder> maps ;
	protected ApplicationContext applicationContext ;

	public void bind(Object obj, Map<String, String> map) throws Exception {
		
		ServiceRegistry serviceRegistry = (ServiceRegistry) obj ;  // "obj"=DefaultServiceRegistry
		
		this.maps = this.applicationContext.getBeansOfType(ServiceHolder.class);
		
		/**
		 * 将自己定义的服务注入到 DefaultServiceRegistry类中，
		 */
		for(ServiceHolder serviceHolder : this.maps.values()){
			serviceRegistry.register(serviceHolder);
		}
	}

	public void unbind(Object obj, Map<String, String> map) throws Exception {
		
		ServiceRegistry serviceRegistry = (ServiceRegistry) obj ;
		for(ServiceHolder serviceHolder : this.maps.values()){
			serviceRegistry.unregister(serviceHolder);
		}
		
	}

	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext ;
	}

}
